/**
 * Created by WangMing on 15/12/9.
 */
define(["../../../service",
    "../../../widgets/breadcrumb_widget/breadcrumb_widget"
], function(api) {
    // 定义所有相关的vmodel
    var vm = avalon.define({
        $id: "album",
        $album: {},
        $name: "fuck",
        $albumVideos: [],
        params: {
            type: "album",
            method: api.getAlbum,
            query: {
                guid: "",
            }

        },
        $breadcrumb: {
            stateName: "albums.item",
            guid: "",
            title: ""
        },
        itemID:"",

        displayPage: function(state) {
            return state.vmName == "album";
        },
        $skipArray: ["breadcrumbWidget", "params", "albumVideos"]
    });


    return avalon.controller(function($ctrl) {

        // 视图渲染后，意思是avalon.scan完成
        $ctrl.$onRendered = function() {
            api.getItems({
                  method:api.setAlbumPv,
                  query:{
                      id:vm.itemID
                  }
              },function(res){
                  
              })
            setTimeout(function(){
                $(window).trigger("imagesDefault");
            },100)
        };
        // 进入视图
        $ctrl.$onEnter = function(param, rs, rj) {
            vm.itemID = param.itemID;

            return new Promise(function(resolve, reject) {
                resolve();
            }).then(function() {
                return api.getItems({
                    method: api.getAlbum,
                    query: {
                        id: param.itemID,
                    }
                }, function(res) {
                    if(res.success){
                        vm.$album = _.merge(vm.$album, res.data);
                        vm.$breadcrumb.title = vm.$album.name||'';
                    }else{
                        vm.$album = null;
                        vm.$breadcrumb.title = null
                    }
                })
            }).then(function() {
                return api.getItems({
                    method: api.getAlbumVideo,
                    query: {
                        albumId: param.itemID,
                        PageSize:10000
                    }
                }, function(res) {
                    if (res.success) {
                        vm.$albumVideos = { 
                            results: {
                                collections: res.list||[]
                            },
                            size: (res.list&&res.list.length)||0,
                            isAjax: false
                        };
                    }else {
                        vm.$albumVideos = null
                    }
                })
            })

        };
        // 对应的视图销毁前
        $ctrl.$onBeforeUnload = function() {

        };
        $ctrl.$vmodels = [vm];
    })
});
